<template>
  <div>
    <!-- 左侧菜单 -->
    <div class="detail_box">
      <!-- 菜单 -->
<!--      <div class="tab_menu">-->
<!--        <div>-->
<!--          <span v-for="(item,index) in menus" :class="{'choose':index===tabIndex}" @click="chooseTab(index)">{{item}}订单</span>-->
<!--        </div>-->
<!--      </div>-->
      <!-- 操作 -->
      <div class="operate_box">
        <div class="item_box">
          <div class="operate_item">
            <label>仓库：</label>
            <el-select
              filterable
              v-model="warehouseId"
              placeholder="请选择仓库">
              <el-option
                v-for="(item,index) in warehouseOption"
                :key="index"
                :label="item.name"
                :value="item.id">
              </el-option>
            </el-select>
          </div>

          <div class="operate_item">
            <label>商品：</label>
            <el-select
              filterable
              v-model="productId"
              placeholder="请选择商品">
              <el-option
                v-for="(item,index) in productOptions"
                :key="index"
                :label="item.name"
                :value="item.id">
              </el-option>
            </el-select>
          </div>

          <div class="operate_item">
            <label>破损单号：</label>
            <el-input type="text" clearable placeholder="请输入破损单号" v-model="wornNo"></el-input>
          </div>

          <div class="operate_item">
            <span @click="getTableInfo(1)" class="operate_btn search_btn">查询</span>
            <span @click="resetTable" class="operate_btn reset_btn">重置</span>
          </div>
        </div>
        <div class="btn_box" v-if="btn&&btn.add">
          <span @click="openPopup('')" class="operate_btn search_btn">新增</span>
        </div>
      </div>
      <!-- 表格 -->
      <div class="flex_box">
        <div class="detail_part" :class="{'left_box':isSee}">
          <div class="table_box">
            <el-table
              :data="tableInfo"
              :header-cell-style="tableHeader"
              border
              ref="multipleTable"
              style="width: 100%;"
              tooltip-effect="dark"
              v-loading="loading"
            >
              <el-table-column
                label="ID"
                prop="id"
                width="80">
              </el-table-column>
              <el-table-column
                label="修改时间"
                prop="1"
                sortable="custom"
                width="160">
                <template slot-scope="scope">
                  <p style="text-align: left">{{ formatTime(scope.row.modifyTime) }}</p>
                </template>
              </el-table-column>
              <el-table-column
                width="220"
                label="破损单号"
                prop="wornNo">
              </el-table-column>

              <el-table-column
                width="200"
                label="仓库">
                <template slot-scope="scope">
                  <span>{{ scope.row.warehouseName }}</span>
                </template>
              </el-table-column>

              <el-table-column
                label="商品名"
                prop="productName">
              </el-table-column>

              <el-table-column
                label="破损数"
                prop="numbers">
              </el-table-column>

<!--              <el-table-column-->
<!--                label="状态"  sortable="custom" prop="2">-->
<!--                <template slot-scope="scope">-->
<!--                  <span :style="'color:'+statusColorOption[scope.row.enable]">{{ statusOptions[scope.row.enable] }}</span>-->
<!--                </template>-->
<!--              </el-table-column>-->


<!--              <el-table-column-->
<!--                fixed="right"-->
<!--                label="操作"-->
<!--                v-if="btn&&(btn.update||btn.delete||btn.confirm||btn.detail)"-->
<!--                width="150">-->
<!--                <template slot-scope="scope">-->
<!--                  <div class="table_btn">-->
<!--                    <span @click="confirmItem(scope.row)" class="change_info success_btn"  v-if="scope.row.enable === 2 && btn.confirm" >确定</span>-->
<!--                    <span @click="function(){isSee=true;orderNo=scope.row.purchaseNo;itemId=scope.row.id}" class="change_info" v-if="btn.detail">详情</span>-->
<!--                    <span @click="openPopup(scope.row.id,isModify=true)" class="change_info" v-if="btn.update && scope.row.enable !== 1">编辑</span>-->
<!--                    <span @click="deleteItem(scope.row)" class="change_info delete_btn" v-if="btn.delete&&scope.row.enable === 1">删除</span>-->
<!--                  </div>-->
<!--                </template>-->
<!--              </el-table-column>-->
            </el-table>
          </div>
          <div class="record_page">
            <span class="page_count">共<i>{{ totals }}</i>条</span>
            <el-pagination
              :current-page="currentPage"
              :page-size="pageSize"
              :page-sizes="[15, 20, 30, 40]"
              :total="totals"
              @current-change="getTableInfo"
              @size-change="sizeChange"
              background
              layout="prev, pager, next, sizes,jumper">
            </el-pagination>
          </div>
        </div>
<!--        <div class="right_box" v-if="isSee">-->
<!--          <p class="page_title">{{orderNo}}订单详情</p>-->
<!--          <detail :orderId="itemId"></detail>-->
<!--          <p class="page_title">{{orderNo}}订单日志</p>-->
<!--          <log :orderId="orderNo"></log>-->
<!--        </div>-->
      </div>

    </div>
    <edit :mid="menuId" :isModify="isModify" @listenToChild="listenToEdit" v-if="isPop"></edit>
  </div>
</template>

<script>
  import edit from "./edit";
  import {getBrokenOrder} from "../../../api/basic/warehouse/brokenOrder";
  import commons from "@/utils/common";
  import {getBtn} from "@/utils/btnAuth";
  import {judgeRole} from "@/utils/warehouseAuth";
  import {listAllData} from "@/api/config/product";

  export default {
    name: 'purchase',
    components: {
      edit,
      // confirmDetail
    },
    data() {
      return {
        productId: '',
        proDate:'',
        plateNumber: '',
        statusOptions: ['不可用','可用','新增'],
        statusColorOption:['#e40000','#67c430','#5EACFC'],
        warehouseOption:[],
        productOptions:[],
        warehouseId:'',
        first: '',
        second: '',
        firstSelect: [],
        secondSelect: [],
        currentPage: 1,
        pageSize: 15,
        totals: 0,
        loading: false,
        tableInfo: [],
        menuId: '',
        isPop: false,
        isModify:false,
        btn: {},
        roleHouse:{},
        warehouse:'',
        isSee:false,
        orderNo:'',
        itemId:'',
        sortNumber:'',
        driverPhone:'',
        wornNo:'',
        menus:['待确认','已完成'],
        tabIndex:0,
        status:2,
        isConfirm:false
      }
    },
    mounted() {
      this.roleHouse = judgeRole();
      this.warehouseOption = this.roleHouse.list;
      this.warehouseId = this.roleHouse.id;
      this.productData();
      this.btn = getBtn();
      if (this.btn.query) {
        this.getTableInfo(1);
      }
    },
    methods: {
      //仓库商品列表
      productData() {
        let that = this;
        listAllData().then(res => {
          that.productOptions = res ? res : [];
        })
      },
      chooseTab(index){
        this.status = index === 0 ? 2 : 1;
        this.tabIndex = index;
        this.getTableInfo(1);
      },

      getTableInfo(page) {
        let that = this;
        that.loading = true;
        that.currentPage = page;
        let queryParams = {
          limit: that.pageSize,
          page: page,
          warehouseId:that.warehouseId,
          productId:that.productId,
          wornNo:that.wornNo
        };
        getBrokenOrder(queryParams).then(res => {
          that.tableInfo = res.list ? res.list : [];
          that.totals = res.count;
          that.loading = false;
        }).catch((e) => {
          that.tableInfo = [];
          that.totals = 0;
          that.loading = false;
        });
      },

      resetTable() {
        this.pageSize = 15;
        this.warehouseId = this.roleHouse.id ? this.warehouseId : '';
        this.productId ='';
        this.wornNo='';
        this.getTableInfo(1)
      },

      // 移除
      deleteItem(obj) {
        let that = this;
        that.$confirm('确定删除「' + obj.purchaseNo + '」进货单信息?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          delPurchaseOrder(obj.id).then(res => {
            that.getTableInfo(that.currentPage);
            that.$message.success({
              message:'删除成功！',
              duration:1500
            });
          }).catch((e) => {

          });
        }).catch(() => {
        });
      },

      //确定
      confirmItem(obj) {
        this.isConfirm = true;
        this.orderId = obj.id
      },

      // 打开弹窗
      openPopup(id) {
        this.isPop = true;
        this.menuId = id;
      },

      listenToEdit(e) {
        this.isPop = false;
        this.isModify=false;
        this.isConfirm = false;
        if (e.refresh) {
          this.getTableInfo(this.currentPage)
        }
      },

      formatTime(time) {
        return commons.formatTime(time)
      },

      sizeChange(val) {
        this.pageSize = val;
        this.getTableInfo(1)
      },

      // 设置table标题居中
      tableHeader({row, column, rowIndex, columnIndex}) {
        return 'background:#F0F2F5;color:#111;padding:10px 0;font-size:14px;font-weight:600;'
      },
    }
  }
</script>

<style scoped>
  .flex_box {
    display: flex;
  }
  .detail_part {
    width: 100%;
  }
  .left_box{
    width: 60%;
  }
  .right_box{
    width: 40%;
  }
  .page_title {
    font-weight: 600;
    padding-left: 20px;
    margin-bottom: 10px;
    font-size: 18px;
  }
</style>
